import VuiElement from '../../base/element';
import VuiSvgContainer from '../../components/icon/svg-container';
import VuiText from '../../components/text';

/*
 * @Description:
 * @Author: Jevon
 * @Date: 2021-11-25 15:11:46
 */
export default class VuiSelectTag extends VuiElement {
  /**
   * 标签
   * @param {*} tag
   * @param {*} tag.value
   * @param {*} tag.text
   * @param {*} deleteFunc
   * @return {*}
   */
  constructor(tag, deleteFunc) {
    super();
    this.tag = tag;
    this._actions = {
      deleteFunc
    };
    this._className = {
      container: 'vui-mtselect-tag',
      text: 'vui-mtselect-tag-text',
      icon: 'vui-mtselect-tag-icon'
    };
  }

  init() {
    const cls = this._className;
    this.initContainer();
    const text = VuiText.newInstance(this.tag.text, cls.text);
    const icon = VuiSvgContainer.newInstance('close', cls.icon);
    icon.setClickListener(evt => {
      this.safeRun(this._actions.deleteFunc, this.tag);
      evt.preventDefault();
      evt.stopPropagation();
    });
    this.append([text, icon]);
    this.tag.container = this.container;
    return this;
  }

  setTag(tag) {
    this.tag = tag;
    this.render();
  }
}
